package com.ry.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ry.novel.mapper.BookCommentMapper;
import com.ry.novel.mapper.BookMapper;
import com.ry.novel.model.dto.BookCommentInfo;
import com.ry.novel.model.dto.BookInfo;
import com.ry.novel.model.dto.UserInfo;
import com.ry.novel.model.dto.condition.CommentQuery;
import com.ry.novel.model.entity.BookComment;
import com.ry.novel.service.BookService;
import com.ry.novel.service.CommentService;
import com.ry.novel.service.ReaderService;
import com.ry.novel.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ry
 * @since 2022-04-10 10:28
 **/
@Service
public class CommentServiceImpl implements CommentService {

    @Autowired
    private BookCommentMapper commentMapper;

    @Autowired
    private ReaderService readerService;

    @Autowired
    private BookService bookService;

    @Override
    public Page<BookCommentInfo> queryComment(CommentQuery commentQuery) {
        Page<BookComment> page = new Page<>(commentQuery.getPage(),commentQuery.getSize());
        Page<BookCommentInfo> infoPage = new Page<>(commentQuery.getPage(),commentQuery.getSize());

        QueryWrapper<BookComment> wrapper = new QueryWrapper<>();
        if (commentQuery.getContent() != null){
            wrapper.like("content",commentQuery.getContent());
        }
        commentMapper.selectPage(page, wrapper);
        List<BookCommentInfo> infos = page.getRecords().stream().map(entity -> {
            BookCommentInfo info = new BookCommentInfo();
            info.setId(entity.getId());
            BookInfo bookInfo = bookService.queryById(entity.getBookId());
            if (bookInfo != null){
                info.setBookId(entity.getBookId());
                info.setBookName(bookInfo.getBookName());
            }
            info.setContent(entity.getContent());
            info.setCreateTime(DateUtil.format(entity.getCreateTime()));
            UserInfo userInfo = readerService.queryById(entity.getUserId());
            if (userInfo != null){
                info.setUserId(userInfo.getId());
                info.setUsername(userInfo.getUsername());
                info.setUser_photo(userInfo.getUserPhoto());
            }

            return info;
        }).collect(Collectors.toList());

        infoPage.setRecords(infos);
        infoPage.setTotal(page.getTotal());

        return infoPage;
    }

    @Override
    public void addComment(BookCommentInfo info) {
        BookComment bookComment = new BookComment();
        bookComment.setBookId(info.getBookId());
        bookComment.setContent(info.getContent());
        bookComment.setCreateTime(LocalDateTime.now().toString());
        bookComment.setUserId(info.getUserId());

        commentMapper.insert(bookComment);
    }

    @Override
    public void updateComment(BookCommentInfo info) {
        BookComment bookComment = new BookComment();
        bookComment.setId(info.getId());
        bookComment.setBookId(info.getBookId());
        bookComment.setContent(info.getContent());
        bookComment.setCreateTime(DateUtil.format(info.getCreateTime()));
        bookComment.setUserId(info.getUserId());

        commentMapper.updateById(bookComment);
    }

    @Override
    public void deleteComment(Long id) {
        commentMapper.deleteById(id);
    }
}
